package com.android.qjpmc.application_module.home.model;

import android.text.TextUtils;

import com.android.qjpmc.bean.AlarmRecordItemBean;
import com.android.qjpmc.bean.GroupInfoPointDataBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.bean.GroupPointHisListBean;
import com.android.qjpmc.bean.PointParameterBean;
import com.android.qjpmc.bean.PointHistoryDataQueryBean;
import com.android.qjpmc.bean.SiteStationBean;
import com.android.qjpmc.bean.StationVideoListBean;
import com.android.qjpmc.widget.bean.BaseListData;
import com.android.qjpmc.widget.bean.BaseRequestData;
import com.android.qjpmc.widget.manager.RxJavaHttpManager;
import com.android.qjpmc.widget.manager.retrofit.RetrofitJsonManager;
import com.android.qjpmc.widget.mvvm.model.BaseModel;
import com.android.qjpmc.widget.retrofithelper.rxschedulers.RxSchedulersHelper;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;

import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

public class StationDataModel extends BaseModel {
    /**
     * 查询站点
     */

    public Observable<List<SiteStationBean>> getStationList() {
        Map<String, Object> map = new HashMap();
        Map<String, Object> pageParams = new HashMap();
        Map<String, Object> queryParams = new HashMap();
        pageParams.put("pageSize", 100);
        pageParams.put("page", 1);
        map.put("pageParams", pageParams);
        map.put("queryParams", queryParams);
        return RetrofitJsonManager.getInstance().getApiService().getStationList(map)
                .compose(RxJavaHttpManager.applyTransformer()).flatMap(new Function<BaseListData<SiteStationBean>, ObservableSource<List<SiteStationBean>>>() {
                    @Override
                    public ObservableSource<List<SiteStationBean>> apply(@NonNull BaseListData<SiteStationBean> siteStationObjBean) throws Exception {
                        List<SiteStationBean> items = siteStationObjBean.items;
                        if (items != null) {
                            for (SiteStationBean item : items) {
                                try {
                                    item.stationId = Integer.parseInt(item.company_id);
                                } catch (Exception e) {
                                    if (!TextUtils.isEmpty(item.company_id)) {
                                        item.stationId = Integer.parseInt(item.company_id.substring(0, 9));
                                    }

                                }
                            }
                            Logger.d("List<SiteStationBean>" + items.size());
                            return Observable.just(items);
                        }
                        return Observable.just(new ArrayList<>());
                    }
                });
    }

    /**
     * 查询站点包含的组
     */

    static public Observable<List<PwrDeviceBean>> getStationOfCompanyAndDevice(String companyCode, String deviceNum) {
        // return Observable.just(new ArrayList<>());
        Map<String, Object> map = new HashMap<>();
        map.put("company_code", companyCode);
        map.put("device_num", deviceNum);
        return RetrofitJsonManager.getInstance().getApiService().getGroupListOfStation(map)
                .compose(RxJavaHttpManager.applyTransformer());
    }

    public Observable<List<PwrDeviceBean>> getGroupListOfStation(String stationId) {
        // return Observable.just(new ArrayList<>());
        Map<String, Object> map = new HashMap<>();
        map.put("company_id", stationId);
        return RetrofitJsonManager.getInstance().getApiService().getGroupListOfStation(map)
                .compose(RxJavaHttpManager.applyTransformer());
    }

    public Observable<List<StationVideoListBean>> getVideoListOfStation(String stationId) {
        // return Observable.just(new ArrayList<>());
        Map<String, Object> map = new HashMap<>();
        map.put("company_id", stationId);
        return RetrofitJsonManager.getInstance().getApiService().getVideoListOfStation(map)
                .compose(RxJavaHttpManager.applyTransformer());
    }

    /**
     * 查询组包含的测点的信息 用于实时数据
     */

    public Observable<List<PointParameterBean>> getPointListOfGroup(String groupId, int type) {
        Map<String, Object> map = new HashMap();
        Map<String, Object> pageParams = new HashMap();
        Map<String, Object> queryParams = new HashMap();
        queryParams.put("device_id", groupId);
        pageParams.put("pageSize", 100);
        pageParams.put("page", 1);
        map.put("pageParams", pageParams);
        map.put("queryParams", queryParams);
        return RetrofitJsonManager.getInstance().getApiService().getPointDataOfGroup(map)
                .compose(RxJavaHttpManager.applyTransformer()).map(new Function<BaseListData<PointParameterBean>, List<PointParameterBean>>() {
                    @Override
                    public List<PointParameterBean> apply(@NonNull BaseListData<PointParameterBean> pointParameterBeanBaseListData) throws Exception {
                        return pointParameterBeanBaseListData.items;
                    }
                });
    }

    /**
     * 查询站点包含的所有测点的实时数据 <List<GroupInfoPointDataBean>
     */

    public Observable<Object> getPointDataOfPointCode(String stationId, List<String> pointList) {
        Map<String, Object> map = new HashMap<>();
        map.put("points", pointList);
        return RetrofitJsonManager.getInstance().getApiService().getPointDataOfPointCode(map)
                .compose(RxJavaHttpManager.applyTransformer());
    }

    /**
     * 查询组包含的所有测点的实时数据  用于实时曲线
     */

    public Observable<List<GroupInfoPointDataBean>> getPointDataOfGroup(String stationId, String groupId, int type) {
        return RetrofitJsonManager.getInstance().getApiService().getPointDataOfGroup(stationId, groupId, type)
                .compose(RxJavaHttpManager.applyTransformer());
    }

    /**
     * 查询站点的指定测点的历史信息
     */

    public Observable<Object> getPointHistoryDataOfStation(Map<String, Object> map) {
        return RetrofitJsonManager.getInstance().getApiService().getPointHistoryDataOfStation(map)
                .compose(RxJavaHttpManager.applyTransformer());
    }

    static public Observable<Object> saveDeviceInfo(Map<String, Object> map) {
        return RetrofitJsonManager.getInstance().getApiService().saveDeviceInfo(map)
                .compose(RxJavaHttpManager.applyTransformer());
    }

    static public Observable<Object> setDataRealtime(Object map) {
        return RetrofitJsonManager.getInstance().getApiService().setDataRealtime(map)
                .compose(RxJavaHttpManager.applyTransformer());
    }

    public static String buildPointCode(PointParameterBean data) {
        return data.deviceCode + "." + data.propsCode;

    }

    public Observable<List<AlarmRecordItemBean>> getAlarmRecordList(Map<String, Object> queryParams) {
        Map<String, Object> map = new HashMap();
        Map<String, Object> pageParams = new HashMap();
        pageParams.put("pageSize", 100);
        pageParams.put("page", 1);
        map.put("pageParams", pageParams);
        map.put("queryParams", queryParams);
        return RetrofitJsonManager.getInstance().getApiService().getAlarmRecordList(map).compose(RxSchedulersHelper.applyIoTransformer()).map(new Function<BaseRequestData<BaseListData<AlarmRecordItemBean>>, List<AlarmRecordItemBean>>() {
            @Override
            public List<AlarmRecordItemBean> apply(@NonNull BaseRequestData<BaseListData<AlarmRecordItemBean>> data) throws Exception {
                if (data.statusCode != 200 || data.data.items == null) {
                    return new ArrayList<AlarmRecordItemBean>();
                }
                return data.data.items;
            }
        });
    }

}
